package com.jb.repair.util;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.jb.repair.R;
import com.jb.repair.common.Constants;
import com.jb.repair.common.DialogCallBack;

/**
 * Created by gaobin on 2015/11/23.
 */
public class MessageUtil {
    public static ProgressDialog progressDialog;
    public static ProgressDialog progressDialog2;

    public static void showToast(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    public static void showLongToast(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }

    public static void showProgressDialog(Context context, String msg) {
        progressDialog = new ProgressDialog(context);
        progressDialog.setMessage(msg);
        progressDialog.setCancelable(true);
        progressDialog.show();
    }

    public static void dismissProgressDialog() {
        progressDialog.dismiss();
    }

    public static void showProgressDialog2(Context context, String msg) {
        if (progressDialog2 != null && progressDialog2.isShowing()) {
            progressDialog2.dismiss();
        }
        progressDialog2 = null;
        Log.i("............", context.toString());
        progressDialog2 = new ProgressDialog(context);
        progressDialog2.setMessage(msg);
        progressDialog2.setCancelable(false);
        progressDialog2.show();
    }

    public static void dismissProgressDialog2() {
        if (progressDialog2 != null && progressDialog2.isShowing()) {
            progressDialog2.dismiss();
        }
    }

    public static void showDialog(final Context context, String title, String message, final int requestViewId, final DialogCallBack onresultListener) {
        final Bundle bundle = new Bundle();
        new AlertDialog.Builder(context).setMessage(message).setTitle(title).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                bundle.putInt("data", Constants.UPDATE_YES);
                onresultListener.onResult(requestViewId, bundle);
                dialog.dismiss();
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                bundle.putInt("data", Constants.UPDATE_NO);
                onresultListener.onResult(requestViewId, bundle);
                dialog.dismiss();
            }
        }).show();
    }

    public static void showToastDialog(final Context context, String title, String message) {
        new AlertDialog.Builder(context).setMessage(message).setTitle(title).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).show();
    }

    public static void showLoginValidataPop(final Context context, View v, final int viewId, final DialogCallBack callBack) {
        View popView = LayoutInflater.from(context).inflate(R.layout.pop_login_validata, null);
        final PopupWindow pop = new PopupWindow(popView, (int) context.getResources().getDimension(R.dimen.x400), ViewGroup.LayoutParams.WRAP_CONTENT, true);
        pop.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        pop.setBackgroundDrawable(dw);
        pop.showAtLocation(v, Gravity.CENTER, 0, 0);
        final EditText et_user_name = (EditText) popView.findViewById(R.id.et_username);
        final EditText et_pwd = (EditText) popView.findViewById(R.id.et_pwd);

        popView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });
        popView.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = et_user_name.getText().toString();
                String pwd = et_pwd.getText().toString();
                String md5_pass = MD5Util.parse(pwd);
                if (StringUtils.isNullOrEmpty(userName) || StringUtils.isNullOrEmpty(pwd)) {
                    Toast.makeText(context, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putString("userName", userName);
                bundle.putString("pwd", md5_pass);
                pop.dismiss();
                callBack.onResult(viewId, bundle);
            }
        });
    }

    public static void showPopDialog(Context context, View v, String title, String msg, String btn_ok_text, final int viewId, final DialogCallBack callBack) {
        View popView = LayoutInflater.from(context).inflate(R.layout.pop_dialog, null);
        final PopupWindow pop = new PopupWindow(popView, (int) context.getResources().getDimension(R.dimen.x400), ViewGroup.LayoutParams.WRAP_CONTENT, true);
        pop.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        pop.setBackgroundDrawable(dw);
        pop.showAtLocation(v, Gravity.CENTER, 0, 0);
        Button btn_ok = (Button) popView.findViewById(R.id.btn_ok);
        TextView tv_title = (TextView) popView.findViewById(R.id.tv_title);
        TextView tv_message = (TextView) popView.findViewById(R.id.tv_message);
        tv_title.setText(title);
        tv_message.setText(msg);
        btn_ok.setText(btn_ok_text);

        popView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putBoolean("data", false);
                callBack.onResult(viewId, bundle);
                pop.dismiss();
            }
        });
        popView.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                pop.dismiss();
                bundle.putBoolean("data", true);
                callBack.onResult(viewId, bundle);
            }
        });
    }

    public static void showRadioPop(Context context, View v, String title, String msg, String btn_ok_text, final int viewId, final DialogCallBack callBack) {
        View popView = LayoutInflater.from(context).inflate(R.layout.pop_first_down, null);
        final PopupWindow pop = new PopupWindow(popView, (int) context.getResources().getDimension(R.dimen.x400), ViewGroup.LayoutParams.WRAP_CONTENT, true);
        pop.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        pop.setBackgroundDrawable(dw);
        pop.showAtLocation(v, Gravity.CENTER, 0, 0);
        Button btn_ok = (Button) popView.findViewById(R.id.btn_ok);
        final ToggleButton tb = (ToggleButton) popView.findViewById(R.id.tb);
        TextView tv_title = (TextView) popView.findViewById(R.id.tv_title);
        TextView tv_message = (TextView) popView.findViewById(R.id.tv_message);
        tv_message.setText(msg);
        tv_title.setText(title);
        btn_ok.setText(btn_ok_text);

        popView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });
        popView.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putBoolean("repeat", tb.isChecked());
                pop.dismiss();
                callBack.onResult(viewId, bundle);
            }
        });
    }

    public static void show2RadioPop(Context context, View v, final int viewId, final DialogCallBack callBack) {
        View popView = LayoutInflater.from(context).inflate(R.layout.pop_2radio, null);
        final PopupWindow pop = new PopupWindow(popView, (int) context.getResources().getDimension(R.dimen.x400), ViewGroup.LayoutParams.WRAP_CONTENT, true);
        pop.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        pop.setBackgroundDrawable(dw);
        pop.showAtLocation(v, Gravity.CENTER, 0, 0);
        final ToggleButton radioButton1 = (ToggleButton) popView.findViewById(R.id.rb1);
        final ToggleButton radioButton2 = (ToggleButton) popView.findViewById(R.id.rb2);
        final Bundle bundle = new Bundle();
        radioButton1.setChecked(true);
        radioButton2.setChecked(false);

        popView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });
        popView.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (radioButton1.isChecked() && !radioButton2.isChecked()) {
                    bundle.putInt("data", 1);  //只同步点检数据
                    pop.dismiss();
                } else if (!radioButton1.isChecked() && radioButton2.isChecked()) {
                    pop.dismiss();
                    bundle.putInt("data", 2);  //只同步台账数据
                } else if (!radioButton1.isChecked() && !radioButton2.isChecked()) {
                    bundle.putInt("data", 3);  //都不同步
                } else {                       //都同步
                    bundle.putInt("data", 0);
                    pop.dismiss();
                }
                callBack.onResult(viewId, bundle);
            }
        });
    }
}